package com.iflytek.partitioner;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

import java.util.HashMap;

public class StatePartitioner extends Partitioner<Text, Text> {

    //模拟图书类型数据字典 实际中可以从redis中快速查询  如果数据不大也可以使用数据集合保存
    public static HashMap<String, Integer> stateMap = new HashMap<>();

    static{
        stateMap.put("Biography", 0);
        stateMap.put("Literature", 1);
        stateMap.put("Economy", 2);
        stateMap.put("Family", 3);
        stateMap.put("Health", 4);
    }

    @Override
    public int getPartition(Text key, Text value, int numPartitions) {
        Integer code = stateMap.get(key.toString());

        if (code != null) {
            return code;
        }

        return 5;
    }
}